"""
Case Type   : 问题回归
Case Name   : 并发执行ts_rank_cd函数
Create At   : 2023/02/15
Owner       : cr13
Description :
    1.循环生成20个线程,并发执行ts_rank_cd函数
Expect      :
    1.成功,返回对应的rank值
History     :
"""

import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_sh = CommonSH('PrimaryDbUser')

    def test_bug_fix(self):
        text = 'step1:循环生成20个线程,并发执行ts_rank_cd函数' \
                ' expect:成功,返回对应的rank值'
        self.logger.info(text)
        sql_cmd = f"select ts_rank_cd(array[0.2,0.3,0.4,0.5], " \
            f"to_tsvector('raining cat and dogs'), 'cat') as rank from to_" \
            f"tsquery('cat') query;"
        threads = []
        for i in range(0, 21):
            session1 = ComThread(self.primary_sh.execut_db_sql,
                                 args=(sql_cmd,))
            threads.append(session1)
        thread_results = []
        for t in threads:
            t.setDaemon(True)
            t.start()
            t.join(10)
            thread_results.append(t.get_result())
        self.logger.info(thread_results)
        for i in thread_results:
            self.assertEqual(str(i.splitlines()[-2]).strip(), '.2', '执行失败'
                             + text)

    def tearDown(self):
        self.logger.info('----无需清理环境----')
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
